package com.ruoyi.fitsculpt.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.fitsculpt.domain.EasyfitUserGoals;
import com.ruoyi.fitsculpt.service.IEasyfitUserGoalsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.ruoyi.fitsculpt.domain.EasyfitUsers;
import com.ruoyi.fitsculpt.service.IEasyfitUsersService;

/**
 * 用户目标Controller
 *
 * @author metaee
 * @date 2025-02-27
 */
@Api(tags = "用户目标管理")
@RestController
@RequestMapping("/fitsculpt/goals")
public class EasyfitUserGoalsController extends BaseController {
    @Autowired
    private IEasyfitUserGoalsService easyfitUserGoalsService;

    @Autowired
    private IEasyfitUsersService easyfitUsersService;

    /**
     * 查询用户目标列表
     */
    @ApiOperation("查询用户目标列表")
    @GetMapping("/list")
    public TableDataInfo list(EasyfitUserGoals easyfitUserGoals) {
        startPage();
        List<EasyfitUserGoals> list = easyfitUserGoalsService.selectEasyfitUserGoalsList(easyfitUserGoals);
        // 关联用户信息
        for (EasyfitUserGoals goal : list) {
            if (goal.getUserId() != null) {
                EasyfitUsers user = easyfitUsersService.selectEasyfitUsersById(goal.getUserId());
                goal.setUser(user);
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出用户目标列表
     */
    @ApiOperation("导出用户目标列表")
    @Log(title = "用户目标", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EasyfitUserGoals easyfitUserGoals) {
        List<EasyfitUserGoals> list = easyfitUserGoalsService.selectEasyfitUserGoalsList(easyfitUserGoals);
        ExcelUtil<EasyfitUserGoals> util = new ExcelUtil<EasyfitUserGoals>(EasyfitUserGoals.class);
        util.exportExcel(response, list, "用户目标数据");
    }

    /**
     * 获取用户目标详细信息
     */
    @ApiOperation("获取用户目标详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        EasyfitUserGoals goal = easyfitUserGoalsService.selectEasyfitUserGoalsById(id);
        if (goal != null && goal.getUserId() != null) {
            EasyfitUsers user = easyfitUsersService.selectEasyfitUsersById(goal.getUserId());
            goal.setUser(user);
        }
        return success(goal);
    }

    /**
     * 新增用户目标
     */
    @ApiOperation("新增用户目标")
    @Log(title = "用户目标", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EasyfitUserGoals easyfitUserGoals) {
        return toAjax(easyfitUserGoalsService.insertEasyfitUserGoals(easyfitUserGoals));
    }

    /**
     * 修改用户目标
     */
    @ApiOperation("修改用户目标")
    @Log(title = "用户目标", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EasyfitUserGoals easyfitUserGoals) {
        return toAjax(easyfitUserGoalsService.updateEasyfitUserGoals(easyfitUserGoals));
    }

    /**
     * 删除用户目标
     */
    @ApiOperation("删除用户目标")
    @Log(title = "用户目标", businessType = BusinessType.DELETE)
    @DeleteMapping("/{goalIds}")
    public AjaxResult remove(@ApiParam(value = "用户目标ID数组", required = true) @PathVariable Long[] goalIds) {
        return toAjax(easyfitUserGoalsService.deleteEasyfitUserGoalsByGoalIds(goalIds));
    }
}
